﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web.Configuration;
using System.Text;

namespace HoaDon
{
	public partial class Invoice : System.Web.UI.Page
	{
		private Table tb_Ban;

		protected void Page_Init(object sender, EventArgs e)
		{
			BindListView_KhuVuc();
			BlindListView_NhomMon();
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			AddEventInButton_Ban();
			AddEventInButton_MonAn();
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				DataTable dtKho = new DataTable();
				string sCommandKho = "SELECT KhoID, KhoName FROM dmkho WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Status = '1'";
				if (Session["ListKhoID"].ToString() != "")
				{
					sCommandKho = sCommandKho + " AND (";
					for (int i = 0; i < Session["ListKhoID"].ToString().Split(',').Length; i++)
					{
						sCommandKho = sCommandKho + "KhoID = '" + Session["ListKhoID"].ToString().Split(',')[i] + "' OR ";
					}
					sCommandKho = sCommandKho + " 1=0)";
				}
				dtKho = SqlEXEC.GetData(Session["strConn"].ToString(), sCommandKho);
				drKho.DataSource = dtKho;
				drKho.DataValueField = "KhoID";
				drKho.DataTextField = "KhoName";
				drKho.DataBind();
				if (Session["Kho"] != null)
				{
					drKho.SelectedValue = Session["Kho"].ToString();
				}
				else
				{
					if (drKho.SelectedValue != null)
						Session["Kho"] = drKho.SelectedValue;
				}

				DataTable dtDoiTac = new DataTable();
				dtDoiTac = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT DoiTacID, DoiTacName, DoiTacTel FROM dmdoitac WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Status = '1' AND Kh_yn = 1 AND (DoiTacID LIKE '%" + txtDoiTac.Text + "%' OR DoiTacName LIKE N'%" + txtDoiTac.Text + "%' OR DoiTacTel LIKE '%" + txtDoiTac.Text + "%')");
				drDoiTac.DataSource = dtDoiTac;
				drDoiTac.DataValueField = "DoiTacID";
				drDoiTac.DataTextField = "DoiTacName";
				drDoiTac.DataBind();

				if (dtDoiTac.Rows.Count > 0)
				{
					drDoiTac.SelectedIndex = 0;
					if (dtDoiTac.Rows[0]["DoiTacTel"].ToString().Trim() != "")
						lblDoiTac.Text = dtDoiTac.Rows[0]["DoiTacID"].ToString().ToUpper() + " - " + dtDoiTac.Rows[0]["DoiTacTel"].ToString();
					else
						lblDoiTac.Text = dtDoiTac.Rows[0]["DoiTacID"].ToString().ToUpper();
				}


				if (Session["btBan"] != null)
				{
					DataTable dtPh = new DataTable();
					dtPh = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM ph81 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND FK_VvID = '" + Session["btBan"].ToString() + "' and Status='0'");

					if ((dtPh.Rows.Count > 0))
					{

						if (dtPh.Rows[0]["FK_doitacID"] != DBNull.Value && drDoiTac.Items.Count > 0)
						{
							drDoiTac.SelectedValue = dtPh.Rows[0]["FK_doitacID"].ToString();
							if (((DataTable)drDoiTac.DataSource).Rows[drDoiTac.SelectedIndex]["DoiTacTel"].ToString() != "")
								lblDoiTac.Text = ((DataTable)drDoiTac.DataSource).Rows[drDoiTac.SelectedIndex]["DoiTacID"].ToString().ToUpper() + " - " + ((DataTable)drDoiTac.DataSource).Rows[drDoiTac.SelectedIndex]["DoiTacTel"].ToString();
							else
								lblDoiTac.Text = ((DataTable)drDoiTac.DataSource).Rows[drDoiTac.SelectedIndex]["DoiTacID"].ToString().ToUpper();
						}
						if (dtPh.Rows[0]["Dien_Giai"] != DBNull.Value)
							txtDienGiai.Text = dtPh.Rows[0]["Dien_Giai"].ToString();
						if (dtPh.Rows[0]["FK_khoID"] != DBNull.Value)
						{
							Session["Kho"] = dtPh.Rows[0]["FK_khoID"].ToString();
							drKho.SelectedValue = dtPh.Rows[0]["FK_khoID"].ToString();
						}
						if (dtPh.Rows[0]["Tien_hang"] != DBNull.Value)
							txtTienHang.Text = Convert.ToDecimal(dtPh.Rows[0]["Tien_hang"]).ToString("#,#0");
						if (dtPh.Rows[0]["Tien_ck"] != DBNull.Value)
							txtCKTong.Text = Convert.ToDecimal(dtPh.Rows[0]["Tien_ck"]).ToString("#,#0");
						if (dtPh.Rows[0]["T_Tien"] != DBNull.Value)
							txtTongTien.Text = Convert.ToDecimal(dtPh.Rows[0]["T_Tien"]).ToString("#,#0");
						if (dtPh.Rows[0]["Tien_kd"] != DBNull.Value)
							txtKhachDua.Text = Convert.ToDecimal(dtPh.Rows[0]["Tien_kd"]).ToString("#,#0");
						if (dtPh.Rows[0]["Tien_tl"] != DBNull.Value)
							txtTraLai.Text = Convert.ToDecimal(dtPh.Rows[0]["Tien_tl"]).ToString("#,#0");
					}
					else
					{
						txtTienHang.Text = "0";
						txtCKTong.Text = "0";
						txtTongTien.Text = "0";
						txtKhachDua.Text = "0";
						txtTraLai.Text = "0";
					}
				}
			}
			BlindGridView_MonDaChon();
		}


		private void BindListView_KhuVuc()
		{
			if (Session["strConn"] == null || Session["FK_DatabaseID"] == null)
				Response.Redirect("~\\Login.aspx", false);
			DataTable dtNhvv = new DataTable();
			dtNhvv = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT NhvvID, NhvvName FROM dmnhvv WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Status = '1'");

			tb_Ban = new Table();
			tbc_ban.Controls.Add(tb_Ban);

			Table tb_nhomban = new Table();
			tbc_nhomban.Controls.Add(tb_nhomban);

			if ((dtNhvv.Rows.Count > 0))
			{
				if (Session["btnhomBan"] == null)
					Session["btnhomBan"] = dtNhvv.Rows[0]["nhvvId"].ToString();
				for (int i = 0; i < dtNhvv.Rows.Count; i++)
				{
					Button btnKhuVuc = new Button();
					btnKhuVuc.Text = dtNhvv.Rows[i]["nhvvName"].ToString();
					btnKhuVuc.ID = "K" + dtNhvv.Rows[i]["nhvvId"].ToString();
					btnKhuVuc.CssClass = "my_btn_kv";
					btnKhuVuc.Click += btKhuvuc_Click;

					TableCell tbcell = new TableCell();
					tbcell.Controls.Add(btnKhuVuc);

					TableRow tbrnhomban = new TableRow();
					tbrnhomban.ID = "nb_" + btnKhuVuc.ID;
					tbrnhomban.Cells.Add(tbcell);

					tb_nhomban.Rows.Add(tbrnhomban);
				}
			}

		}

		private void BlindListView_NhomMon()
		{
			DataTable dtNhvt2 = new DataTable();
			dtNhvt2 = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT Nhvt2ID, Nhvt2Name FROM dmnhvt2 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND TP_yn = 1 AND NL_yn = 0 AND Status = 1");

			if ((dtNhvt2.Rows.Count > 0))
			{
				if (Session["btnhomMon"] == null)
					Session["btnhomMon"] = dtNhvt2.Rows[0]["nhvt2Id"].ToString();
				for (int i = 0; i < dtNhvt2.Rows.Count; i++)
				{
					Button btnNhomMon = new Button();
					btnNhomMon.Text = dtNhvt2.Rows[i]["nhvt2Name"].ToString();
					btnNhomMon.ID = "N" + dtNhvt2.Rows[i]["nhvt2Id"].ToString();
					btnNhomMon.CssClass = "my_btn_nhommon";
					btnNhomMon.Click += btNhomMon_Click;
					tbc_nhommon.Controls.Add(btnNhomMon);
				}
			}
		}

		private void BlindGridView_MonDaChon()
		{
			if (Session["btBan"] != null)
			{
				if (Session["change"].ToString().Equals("0"))
				{
					DataTable dtPh = new DataTable();
					string ban = Session["btBan"].ToString();
					dtPh = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM ph81 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND FK_VvID = '" + ban + "' and Status='0'");

					if ((dtPh.Rows.Count > 0))
					{
						string Stt_rec = dtPh.Rows[0]["Stt_rec"].ToString();
						Session["Stt_rec"] = Stt_rec;

						DataTable dtCt = new DataTable();
						dtCt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM vct81 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Stt_rec = '" + Stt_rec + "'");
						gdxdsmon.DataSource = dtCt;
						gdxdsmon.DataBind();
						Session["dt"] = dtCt;
					}
					else
					{
						Session["dt"] = null;
					}
				}
				else
					if (Session["dt"] != null)
					{
						gdxdsmon.DataSource = Session["dt"] as DataTable;
						gdxdsmon.DataBind();
						if (Session["btMonan"] != null && Session["change"].ToString().Equals("1"))
						{
							DataTable dt = Session["dt"] as DataTable;
							string monan = Session["btMonan"].ToString();
							for (int i = 0; i < dt.Rows.Count; i++)
								if (dt.Rows[i]["FK_VtID"].ToString().Equals(monan))
								{
									//decimal ck = Convert.ToDecimal(dt.Rows[i]["Ck"].ToString());
									decimal soluong = Convert.ToDecimal(dt.Rows[i]["So_luong"].ToString()) + 1;
									decimal tien = soluong * Convert.ToDecimal(dt.Rows[i]["Gia"].ToString());

									dt.Rows[i]["So_luong"] = soluong;
									dt.Rows[i]["Tien"] = tien;
									gdxdsmon.DataSource = dt;
									gdxdsmon.DataBind();

									Session["btMonan"] = null;
									Session["dt"] = dt;
									Session["change"] = "2";
								}
							if (Session["btMonan"] != null)
							{
								string vtu = Session["btMonan"].ToString();
								string tenmonan = Session["btNameMonAn"].ToString();
								string dvt_gia = Session["btDvt_gia"].ToString();
								string[] s = dvt_gia.Split('_');
								string dvt = s[0];
								string gia = s[1];
								decimal sol = 1;
								decimal nguyengia = Convert.ToDecimal(gia);
								decimal thanhtien = sol * nguyengia;

								DataRow NewRow = dt.NewRow();
								NewRow["Stt_rec"] = Session["Stt_rec"].ToString();
								NewRow["Stt_rec0"] = GetRec0(dt.Rows.Count + 1);
								NewRow["FK_DatabaseID"] = Session["FK_DatabaseID"].ToString();
								NewRow["FK_vtID"] = vtu;
								NewRow["VtName"] = tenmonan;
								NewRow["FK_dvtID"] = dvt;
								NewRow["FK_khoID"] = drKho.SelectedValue.ToString();
								NewRow["So_luong"] = sol;

								NewRow["Nguyen_gia"] = gia;
								NewRow["Ck"] = 0;
								NewRow["Gia_ck"] = 0;
								NewRow["Tien_cong"] = 0;
								NewRow["Gia"] = gia;
								NewRow["Tien"] = thanhtien;

								NewRow["FK_TkID_dt"] = "5111";
								NewRow["FK_TkID_vt"] = "156";
								NewRow["FK_TkID_gv"] = "632";
								NewRow["s3"] = System.Web.HttpContext.Current.User.Identity.Name.ToUpper();
								NewRow["user_id0"] = System.Web.HttpContext.Current.User.Identity.Name.ToUpper();
								NewRow["user_id2"] = System.Web.HttpContext.Current.User.Identity.Name.ToUpper();
								NewRow["datetime0"] = DateTime.Now.Date;
								NewRow["datetime2"] = DateTime.Now.Date;

								dt.Rows.Add(NewRow);

								gdxdsmon.DataSource = dt;
								gdxdsmon.DataBind();
								Session["change"] = "2";

								Session["btMonan"] = null;
								Session["dt"] = dt;
							}
						}
					}

					else
					{
						if (Session["btMonan"] != null)
						{
							string vtu = Session["btMonan"].ToString();
							string tenmonan = Session["btNameMonAn"].ToString();
							string dvt_gia = Session["btDvt_gia"].ToString();
							string[] s = dvt_gia.Split('_');
							string dvt = s[0];
							string gia = s[1];
							decimal sol = 1;
							decimal nguyengia = Convert.ToDecimal(gia);
							decimal thanhtien = sol * nguyengia;

							DataTable dt = new DataTable();
							DataColumn dc = new DataColumn();

							dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT TOP 0 * FROM vct81");

							DataRow NewRow = dt.NewRow();
							Session["Stt_rec"] = GetRecID("HDA");
							NewRow["Stt_rec"] = Session["Stt_rec"].ToString();
							NewRow["Stt_rec0"] = GetRec0(dt.Rows.Count + 1);
							NewRow["FK_DatabaseID"] = Session["FK_DatabaseID"].ToString();
							NewRow["FK_vtID"] = vtu;
							NewRow["VtName"] = tenmonan;
							NewRow["FK_dvtID"] = dvt;
							NewRow["FK_khoID"] = drKho.SelectedValue.ToString();
							NewRow["So_luong"] = sol;

							NewRow["Nguyen_gia"] = gia;
							NewRow["Ck"] = 0;
							NewRow["Gia_ck"] = 0;
							NewRow["Tien_cong"] = 0;
							NewRow["Gia"] = gia;
							NewRow["Tien"] = thanhtien;

							NewRow["FK_TkID_dt"] = "5111";
							NewRow["FK_TkID_vt"] = "156";
							NewRow["FK_TkID_gv"] = "632";
							NewRow["s3"] = System.Web.HttpContext.Current.User.Identity.Name.ToUpper();
							NewRow["user_id0"] = System.Web.HttpContext.Current.User.Identity.Name.ToUpper();
							NewRow["user_id2"] = System.Web.HttpContext.Current.User.Identity.Name.ToUpper();
							NewRow["datetime0"] = DateTime.Now.Date;
							NewRow["datetime2"] = DateTime.Now.Date;

							dt.Rows.Add(NewRow);

							gdxdsmon.DataSource = dt;
							gdxdsmon.DataBind();
							Session["dt"] = dt;
							Session["change"] = "2";
							Session["btMonan"] = null;
						}
					}

				//button giam duoc click
				if (Session["change"].ToString().Equals("3"))
				{
					//da co du lieu tren girdview 
					if (Session["dt"] != null && Session["Stt_Rec0"] != null)
					{
						DataTable dt = Session["dt"] as DataTable;
						if (dt.Rows.Count > 0)
						{
							for (int i = 0; i < dt.Rows.Count; i++)
							{
								if (dt.Rows[i]["Stt_Rec0"].ToString().Equals(Session["Stt_Rec0"].ToString()))
								{
									decimal soluong = Convert.ToDecimal(dt.Rows[i]["So_luong"].ToString()) - 1;
									decimal gia = Convert.ToDecimal(dt.Rows[i]["Gia"].ToString());
									decimal tien = soluong * gia;
									dt.Rows[i]["So_luong"] = soluong;
									dt.Rows[i]["Tien"] = tien;
									if (soluong == 0)
									{
										dt.Rows[i].Delete();
										dt.AcceptChanges();
									}
								}
							}
						}
						gdxdsmon.DataSource = dt;
						gdxdsmon.DataBind();
						Session["btMonan"] = null;
						//da giam mon an
						Session["dt"] = dt;
						Session["change"] = "2";
					}
				}

				decimal tienhang = 0;
				//decimal tienck = 0;
				for (int i = 0; i < gdxdsmon.Rows.Count; i++)
				{
					if (((Label)gdxdsmon.Rows[i].FindControl("Tien")).Text.Trim() != "")
						tienhang += Convert.ToDecimal(((Label)gdxdsmon.Rows[i].FindControl("Tien")).Text.ToString());
				}
				txtTienHang.Text = tienhang.ToString("#,#0");

				string cktong = txtCKTong.Text.Replace(".", "").Replace(",", "");
				if (cktong == "")
					cktong = "0";
				string khachdua = txtKhachDua.Text.Replace(".", "").Replace(",", "");
				if (khachdua == "")
					khachdua = "0";

				txtTongTien.Text = (tienhang - Convert.ToDecimal(cktong)).ToString("#,#0");
				string tongtien = txtTongTien.Text.Replace(".", "").Replace(",", "");
				if (tongtien == "")
					tongtien = "0";
				txtTraLai.Text = (Convert.ToDecimal(khachdua) - Convert.ToDecimal(tongtien)).ToString("#,#0");
			}
		}

		//Get Stt_rec ung voi hoa don ban hang "HDA"
		private string GetRecID(string cVoucherCode)
		{
			try
			{
				DataTable dtstt = new DataTable();
				dtstt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT RecNumber FROM dmstt WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "'");
				double nOrder = Convert.ToDouble(dtstt.Rows[0]["RecNumber"]) + 1;
				string RecID = Convert.ToString(nOrder);
				int i = 9;
				int j = RecID.Trim().Length;
				while ((j < i))
				{
					RecID = "0" + RecID;
					j = j + 1;
				}
				DataTable dtopp = new DataTable();
				dtopp = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT val FROM options WHERE name = 'm_WrkStation'");
				RecID = dtopp.Rows[0]["val"].ToString() + RecID + cVoucherCode.Trim().ToUpper();
				SqlEXEC.ExecCommand(Session["strConn"].ToString(), "UPDATE dmstt SET RecNumber = " + Convert.ToString(nOrder + 1) + " WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "'");
				return RecID;
			}
			catch
			{
				return "";
			}
		}

		private string GetRec0(int iStt)
		{
			if (iStt < 10)
				return "00" + iStt.ToString();
			if (iStt < 100 && iStt >= 10)
				return "0" + iStt.ToString();
			return iStt.ToString();
		}

		private void AddEventInButton_MonAn()
		{
			if (Session["btnhomMon"] != null)
			{
				string nhommon = Session["btnhomMon"].ToString();

				DataTable ds = new DataTable();
				ds = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM dmvt WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND FK_Nhvt2ID = '" + nhommon + "' AND Status = 1");

				if ((ds.Rows.Count > 0))
				{
					for (int i = 0; i < ds.Rows.Count; i++)
					{
						Button btnMon = new Button();
						btnMon.Text = ds.Rows[i]["vtName"].ToString();
						btnMon.ID = "M" + ds.Rows[i]["vtID"].ToString();
						btnMon.CommandArgument = ds.Rows[i]["FK_DvtID"].ToString() + "_" + ds.Rows[i]["Gia_ban0"].ToString();
						btnMon.CssClass = "my_btn_mon";
						btnMon.Click += btMonan_Click;
						tbc_mon.Controls.Add(btnMon);
					}
				}
			}
		}

		private void AddEventInButton_Ban()
		{
			if (Session["btnhomBan"] != null)
			{
				string nhomban = Session["btnhomBan"].ToString();

				DataTable ds = new DataTable();
				ds = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM dmvv WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND FK_NhvvID = '" + nhomban + "'");

				TableCell tbcell = new TableCell();
				TableRow tbrow = new TableRow();
				//tbcell.ControlStyle.CssClass = "mycell";

				if ((ds.Rows.Count > 0))
				{
					for (int i = 0; i < ds.Rows.Count; i++)
					{
						Button btnn = new Button();
						btnn.Text = ds.Rows[i]["vvName"].ToString();
						btnn.ID = "B" + ds.Rows[i]["vvID"].ToString();
						DataTable ds1 = new DataTable();
						ds1 = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM ph81 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND FK_VvID = '" + btnn.ID.Substring(1) + "' AND Status = '0'");
						if (ds1.Rows.Count > 0)
							btnn.CssClass = "my_btn_0";
						else
							btnn.CssClass = "my_btn_1";
						if (Session["btBan"] != null)
							if (btnn.ID.Substring(1) == Session["btBan"].ToString())
								btnn.CssClass = "my_btn_2";

						btnn.CausesValidation = false;
						btnn.Click += btBan_Click;

						tbcell.Controls.Add(btnn);
						tbrow.Cells.Add(tbcell);
						tb_Ban.Rows.Add(tbrow);
					}
				}
			}
		}

		private void btKhuvuc_Click(Object sender, EventArgs e)
		{
			Button btn = (Button)sender;
			Session["btnhomBan"] = btn.ID.Substring(1);
			Session["btBan"] = null;
			Session["change"] = "2";
			Session["dt"] = null;
			//Session["btnhomMon"] = null;
			Session["btMonan"] = null;
			Session["btNameMonAn"] = null;
			Session["btDvt_gia"] = null;
			Session["Stt_Rec"] = null;
			Session["STT_Rec0"] = null;
			Response.Redirect("~\\Invoice.aspx");
		}

		private void btBan_Click(Object sender, EventArgs e)
		{
			Button btn = (Button)sender;
			Session["btBan"] = btn.ID.Substring(1);
			Session["change"] = "0";
			Session["dt"] = null;
			//Session["btnhomMon"] = null;
			Session["btMonan"] = null;
			Session["btNameMonAn"] = null;
			Session["btDvt_gia"] = null;
			Session["Stt_Rec"] = null;
			Session["STT_Rec0"] = null;
			Response.Redirect("~\\Invoice.aspx");
		}

		private void btNhomMon_Click(Object sender, EventArgs e)
		{
			Button btn = (Button)sender;
			Session["btnhomMon"] = btn.ID.Substring(1);
			Session["btMonan"] = null;
			Session["btNameMonAn"] = null;
			Session["btDvt_gia"] = null;
			Response.Redirect("~\\Invoice.aspx");
		}

		private void btMonan_Click(Object sender, EventArgs e)
		{
			Button btn = (Button)sender;
			Session["btMonan"] = btn.ID.Substring(1);
			Session["btNameMonAn"] = btn.Text;
			Session["btDvt_gia"] = btn.CommandArgument;
			Session["change"] = "1";
			//Response.Redirect("~\\Invoice.aspx");
		}

		protected void btthoanhtoan_Click(object sender, EventArgs e)
		{
			if (drKho.Items.Count <= 0)
			{
				Response.Write("<script>alert('Chưa có dữ liệu về kho, cửa hàng, không thể lưu hóa đơn');</script>");
				return;
			}
			if (drDoiTac.Items.Count <= 0)
			{
				Response.Write("<script>alert('Chưa có dữ liệu về khách hàng, không thể lưu hóa đơn');</script>");
				return;
			}
			if (Session["Stt_Rec"] != null)
			{
				Save();
				SqlEXEC.ExecCommand(Session["strConn"].ToString(), "UPDATE ph81 SET Status= '1' WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Stt_rec ='" + Session["Stt_rec"].ToString() + "' ");
				Post(Session["Stt_rec"].ToString());
				//TaoPhieuXuatKho();

				//Session["btnhomBan"] = null;
				Session["btBan"] = null;
				Session["change"] = "2";
				Session["dt"] = null;
				//Session["btnhomMon"] = null;
				Session["btMonan"] = null;
				Session["btNameMonAn"] = null;
				Session["btDvt_gia"] = null;
				Session["Stt_Rec"] = null;
				Session["STT_Rec0"] = null;
				Response.Redirect("~\\Invoice.aspx");
			}
		}

		private void TaoPhieuXuatKho()
		{
			string so_ct = "";
			string stt_rec = "";
			try
			{
				stt_rec = GetRecID("PXA");

				DataTable dtSoCT = new DataTable();
				string text2 = "EXEC fs_HG_GET_SCT 'PXA','" + Session["FK_DatabaseID"].ToString() + "'";
				dtSoCT = SqlEXEC.GetData(Session["strConn"].ToString(), text2);
				so_ct = dtSoCT.Rows[0]["So_ct"].ToString();

				SqlEXEC.ExecCommand(Session["strConn"].ToString(), "UPDATE dmct SET No = No + 1 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND CtID = 'PXA'");
				SqlEXEC.ExecCommand(Session["strConn"].ToString(), "exec fs_InsertPXA_FromHDA '" + Session["Stt_rec"] + "','" + stt_rec + "','" + so_ct + "','" + DateTime.Now.ToString("yyyyMMdd") + "','" + Session["FK_DatabaseID"].ToString() + "'");
			}
			catch
			{
			}
		}

		private void Post(string sSTT_Rec)
		{
			try
			{
				string text1 = "EXEC fs_PostHDA2Inventory ";
				text1 = (text1 + "'" + "HDA" + "'");
				text1 = (text1 + ", '" + "ph81" + "'");
				text1 = (text1 + ", '" + "ct81" + "'");
				text1 = (text1 + ", '" + sSTT_Rec + "'");
				text1 = (text1 + ", '" + "1" + "'");
				text1 = (text1 + ", '" + Session["FK_DatabaseID"].ToString() + "'");
				SqlEXEC.ExecCommand(Session["strConn"].ToString(), text1);
			}
			catch
			{
			}
		}

		protected void btLuu_Click(object sender, EventArgs e)
		{
			if (drKho.Items.Count <= 0)
			{
				Response.Write("<script>alert('Chưa có dữ liệu về kho, cửa hàng, không thể lưu hóa đơn');</script>");
				return;
			}
			if (drDoiTac.Items.Count <= 0)
			{
				Response.Write("<script>alert('Chưa có dữ liệu về khách hàng, không thể lưu hóa đơn');</script>");
				return;
			}

			Save();

			//Session["btnhomBan"] = null;
			//Session["btBan"] = null;
			//Session["change"] = "2";
			//Session["dt"] = null;
			//Session["btnhomMon"] = null;
			Session["btMonan"] = null;
			Session["btNameMonAn"] = null;
			Session["btDvt_gia"] = null;
			//Session["Stt_Rec"] = null;
			//Session["STT_Rec0"] = null;
			Response.Redirect("~\\Invoice.aspx");
		}

		protected void Save()
		{
			if (Session["Stt_Rec"] != null)
			{
				DataTable ds = new DataTable();
				ds = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM ph81 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Stt_rec = '" + Session["Stt_Rec"].ToString() + "'");

				if (ds.Rows.Count > 0)
				{
					if (Session["dt"] != null)
					{
						decimal tien_ck = 0;
						string s_tien_ck = txtCKTong.Text.Replace(".", "").Replace(",", "");
						if (s_tien_ck == "")
							s_tien_ck = "0";
						tien_ck = Convert.ToDecimal(s_tien_ck);

						DataTable dtCal = (DataTable)Session["dt"];
						decimal tienhang = 0;
						decimal soluong = 0;

						for (int i = 0; i < dtCal.Rows.Count; i++)
						{
							soluong = soluong + Convert.ToDecimal(dtCal.Rows[i]["So_luong"]);
							tienhang = tienhang + Convert.ToDecimal(dtCal.Rows[i]["Tien"]);
						}


						string khachdua = txtKhachDua.Text.Replace(".", "").Replace(",", "");
						if (khachdua == "")
							khachdua = "0";

						string tralai = txtTraLai.Text.Replace(".", "").Replace(",", "");
						if (tralai == "")
							tralai = "0";

						Session["SoCT"] = ds.Rows[0]["So_ct"].ToString();
						string sPh81 = "UPDATE ph81 SET ";
						sPh81 = sPh81 + "Dien_Giai = N'" + txtDienGiai.Text + "', ";
						if (drDoiTac.Items.Count > 0 && drDoiTac.SelectedValue != null)
							sPh81 = sPh81 + "FK_doitacID = '" + drDoiTac.SelectedValue.ToString() + "', ";
						sPh81 = sPh81 + "FK_khoID = '" + Session["Kho"].ToString() + "', ";
						sPh81 = sPh81 + "T_Sl = " + soluong + ", ";
						sPh81 = sPh81 + "Tien_ck = " + tien_ck + ", ";
						sPh81 = sPh81 + "Ck_tong = " + "0" + ", ";
						sPh81 = sPh81 + "hs_thue = " + "0" + ", ";
						sPh81 = sPh81 + "Tien_thue = " + "0" + ", ";
						sPh81 = sPh81 + "Tien_hang = " + tienhang + ", ";
						sPh81 = sPh81 + "T_Tien = " + (tienhang - tien_ck) + ", ";
						sPh81 = sPh81 + "user_id2 = '" + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "', ";
						sPh81 = sPh81 + "s3 = '" + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "', ";
						sPh81 = sPh81 + "datetime2 = '" + DateTime.Now.ToString("yyyyMMdd") + "', ";
						sPh81 = sPh81 + "Tien_kd = " + khachdua + ", ";
						sPh81 = sPh81 + "Tien_tl = " + tralai + " ";

						sPh81 = sPh81 + "WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Stt_rec = '" + Session["Stt_rec"].ToString() + "'";
						SqlEXEC.ExecCommand(Session["strConn"].ToString(), sPh81);

						DataTable dt = Session["dt"] as DataTable;
						SqlEXEC.ExecCommand(Session["strConn"].ToString(), "DELETE FROM ct81 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Stt_Rec = '" + Session["Stt_Rec"].ToString() + "'");
						for (int i = 0; i < dt.Rows.Count; i++)
						{
							string sCommand = "INSERT INTO ct81 (Stt_rec, Stt_rec0, FK_DatabaseID, FK_vtID, FK_dvtID, FK_KhoID, Dien_giai, So_luong, Nguyen_gia, Ck, Gia_ck, Tien_cong, Gia, Tien, FK_TkID_dt, FK_TkID_vt, FK_TkID_gv, s3, user_id0, user_id2, datetime0, datetime2) VALUES ('";
							sCommand = sCommand + Session["Stt_Rec"].ToString() + "','";
							sCommand = sCommand + dt.Rows[i]["Stt_rec0"].ToString() + "','";
							sCommand = sCommand + Session["FK_DatabaseID"].ToString() + "','";
							sCommand = sCommand + dt.Rows[i]["FK_vtID"].ToString() + "',N'";
							sCommand = sCommand + dt.Rows[i]["FK_dvtID"].ToString() + "','";
							sCommand = sCommand + dt.Rows[i]["FK_KhoID"].ToString() + "',N'";
							sCommand = sCommand + "" + "',";
							sCommand = sCommand + dt.Rows[i]["So_luong"].ToString() + ",";
							sCommand = sCommand + dt.Rows[i]["Nguyen_gia"].ToString() + ",";
							sCommand = sCommand + dt.Rows[i]["Ck"].ToString() + ",";
							sCommand = sCommand + dt.Rows[i]["Gia_ck"].ToString() + ",";
							sCommand = sCommand + dt.Rows[i]["Tien_cong"].ToString() + ",";
							sCommand = sCommand + dt.Rows[i]["Gia"].ToString() + ",";
							sCommand = sCommand + dt.Rows[i]["Tien"].ToString() + ",'";
							sCommand = sCommand + dt.Rows[i]["FK_TkID_dt"].ToString() + "','";
							sCommand = sCommand + dt.Rows[i]["FK_TkID_vt"].ToString() + "','";
							sCommand = sCommand + dt.Rows[i]["FK_TkID_gv"].ToString() + "','";
							sCommand = sCommand + dt.Rows[i]["s3"].ToString() + "','";
							sCommand = sCommand + dt.Rows[i]["user_id0"].ToString() + "','";
							sCommand = sCommand + dt.Rows[i]["user_id2"].ToString() + "','";
							sCommand = sCommand + Convert.ToDateTime(dt.Rows[i]["datetime0"]).ToString("yyyyMMdd") + "','";
							sCommand = sCommand + Convert.ToDateTime(dt.Rows[i]["datetime2"]).ToString("yyyyMMdd") + "')";
							SqlEXEC.ExecCommand(Session["strConn"].ToString(), sCommand);
						}
					}
				}
				else
				{
					if (Session["dt"] != null)
					{
						DataTable dt = Session["dt"] as DataTable;
						if (dt.Rows.Count > 0)
						{
							DataTable Ds2 = new DataTable();
							string text2 = "EXEC fs_HG_GET_SCT 'HDA','" + Session["FK_DatabaseID"].ToString() + "'";
							Ds2 = SqlEXEC.GetData(Session["strConn"].ToString(), text2);
							SqlEXEC.ExecCommand(Session["strConn"].ToString(), "UPDATE dmct SET No = (No + 1) WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND CtID = 'HDA'");
							Session["SoCT"] = Ds2.Rows[0]["So_ct"].ToString();

							decimal tien_ck = 0;
							string s_tien_ck = txtCKTong.Text.Replace(".", "").Replace(",", "");
							if (s_tien_ck == "")
								s_tien_ck = "0";
							tien_ck = Convert.ToDecimal(s_tien_ck);

							DataTable dtCal = (DataTable)Session["dt"];
							decimal tienhang = 0;
							decimal soluong = 0;
							for (int i = 0; i < dtCal.Rows.Count; i++)
							{
								soluong = soluong + Convert.ToDecimal(dtCal.Rows[i]["So_luong"]);
								tienhang = tienhang + Convert.ToDecimal(dtCal.Rows[i]["Tien"]);
							}

							string khachdua = txtKhachDua.Text.Replace(".", "").Replace(",", "");
							if (khachdua == "")
								khachdua = "0";

							string tralai = txtTraLai.Text.Replace(".", "").Replace(",", "");
							if (tralai == "")
								tralai = "0";

							string sPh81 = "INSERT INTO ph81 (Stt_rec, FK_DatabaseID, FK_CtID, Ngay_ct, So_ct, Code, Ong_Ba, Dia_Chi, Dien_Giai, FK_NxID, FK_TkID, FK_doitacID, FK_khoID, FK_bpkdID, s3, FK_VvID, FK_NhomgiaID, FK_TkCKID, FK_TkVATID, FK_VatID, T_Sl, Tien_ck, Ck_tong, hs_thue, Tien_thue, Tien_hang, T_Tien, Status, user_id0, user_id2, datetime0, datetime2, FK_KhlID, Tien_kd, Tien_tl) VALUES('";
							sPh81 = sPh81 + Session["Stt_Rec"].ToString() + "','";
							sPh81 = sPh81 + Session["FK_DatabaseID"].ToString() + "','";
							sPh81 = sPh81 + "HDA" + "','";
							sPh81 = sPh81 + DateTime.Now.ToString("yyyyMMdd") + "','";
							sPh81 = sPh81 + Session["SoCT"].ToString() + "',N'',N'',N'',N'" + txtDienGiai.Text + "','BTM','1111','";
							if (drDoiTac.Items.Count > 0 && drDoiTac.SelectedValue != null)
								sPh81 = sPh81 + drDoiTac.SelectedValue.ToString() + "','";
							else
								sPh81 = sPh81 + "AKL" + "','";
							sPh81 = sPh81 + Session["Kho"].ToString() + "','";
							sPh81 = sPh81 + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "','";
							sPh81 = sPh81 + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "','";
							sPh81 = sPh81 + Session["btBan"].ToString() + "','','5211','133','0',";

							sPh81 = sPh81 + soluong + ",";
							sPh81 = sPh81 + tien_ck + ",";
							sPh81 = sPh81 + "0" + ",";
							sPh81 = sPh81 + "0" + ",";
							sPh81 = sPh81 + "0" + ",";
							sPh81 = sPh81 + tienhang + ",";
							sPh81 = sPh81 + (tienhang - tien_ck) + ",'";

							sPh81 = sPh81 + "0" + "','";
							sPh81 = sPh81 + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "','";
							sPh81 = sPh81 + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "','";
							sPh81 = sPh81 + DateTime.Now.ToString("yyyyMMdd") + "','";
							sPh81 = sPh81 + DateTime.Now.ToString("yyyyMMdd") + "','";
							sPh81 = sPh81 + "AKL" + "',";
							sPh81 = sPh81 + khachdua + ",";
							sPh81 = sPh81 + tralai + ")";
							SqlEXEC.ExecCommand(Session["strConn"].ToString(), sPh81);

							for (int i = 0; i < dt.Rows.Count; i++)
							{
								string sCommand = "INSERT INTO ct81 (Stt_rec, Stt_rec0, FK_DatabaseID, FK_vtID, FK_dvtID, FK_KhoID, Dien_giai, So_luong, Nguyen_gia, Ck, Gia_ck, Tien_cong, Gia, Tien, FK_TkID_dt, FK_TkID_vt, FK_TkID_gv, s3, user_id0, user_id2, datetime0, datetime2) VALUES ('";
								sCommand = sCommand + Session["Stt_Rec"].ToString() + "','";
								sCommand = sCommand + dt.Rows[i]["Stt_rec0"].ToString() + "','";
								sCommand = sCommand + Session["FK_DatabaseID"].ToString() + "','";
								sCommand = sCommand + dt.Rows[i]["FK_vtID"].ToString() + "',N'";
								sCommand = sCommand + dt.Rows[i]["FK_dvtID"].ToString() + "','";
								sCommand = sCommand + dt.Rows[i]["FK_KhoID"].ToString() + "',N'";
								sCommand = sCommand + "" + "',";
								sCommand = sCommand + dt.Rows[i]["So_luong"].ToString() + ",";
								sCommand = sCommand + dt.Rows[i]["Nguyen_gia"].ToString() + ",";
								sCommand = sCommand + dt.Rows[i]["Ck"].ToString() + ",";
								sCommand = sCommand + dt.Rows[i]["Gia_ck"].ToString() + ",";
								sCommand = sCommand + dt.Rows[i]["Tien_cong"].ToString() + ",";
								sCommand = sCommand + dt.Rows[i]["Gia"].ToString() + ",";
								sCommand = sCommand + dt.Rows[i]["Tien"].ToString() + ",'";
								sCommand = sCommand + dt.Rows[i]["FK_TkID_dt"].ToString() + "','";
								sCommand = sCommand + dt.Rows[i]["FK_TkID_vt"].ToString() + "','";
								sCommand = sCommand + dt.Rows[i]["FK_TkID_gv"].ToString() + "','";
								sCommand = sCommand + dt.Rows[i]["s3"].ToString() + "','";
								sCommand = sCommand + dt.Rows[i]["user_id0"].ToString() + "','";
								sCommand = sCommand + dt.Rows[i]["user_id2"].ToString() + "','";
								sCommand = sCommand + Convert.ToDateTime(dt.Rows[i]["datetime0"]).ToString("yyyyMMdd") + "','";
								sCommand = sCommand + Convert.ToDateTime(dt.Rows[i]["datetime2"]).ToString("yyyyMMdd") + "')";
								SqlEXEC.ExecCommand(Session["strConn"].ToString(), sCommand);
							}
						}
					}
				}

			}
		}

		protected void btBoqua_Click(object sender, EventArgs e)
		{
			//Session["btnhomBan"] = null;
			//Session["btBan"] = null;
			Session["change"] = "0";
			//Session["dt"] = null;
			//Session["btnhomMon"] = null;
			Session["btMonan"] = null;
			Session["btNameMonAn"] = null;
			Session["btDvt_gia"] = null;
			//Session["Stt_Rec"] = null;
			Session["STT_Rec0"] = null;
			Response.Redirect("~\\Invoice.aspx");
		}

		protected void btTimLai_Click(object sender, EventArgs e)
		{
			Response.Redirect("~\\LocHoaDon.aspx");
		}

		protected void btXoa_Click(object sender, EventArgs e)
		{
			if (drKho.Items.Count <= 0)
				return;

			if (Session["Stt_Rec"] != null)
			{
				SqlEXEC.ExecCommand(Session["strConn"].ToString(), "EXEC [fs_DeleteHDA] '" + Session["Stt_Rec"].ToString() + "', '" + Session["FK_DatabaseID"].ToString() + "', '1'");

				//Session["btnhomBan"] = null;
				Session["btBan"] = null;
				Session["change"] = "2";
				Session["dt"] = null;
				//Session["btnhomMon"] = null;
				Session["btMonan"] = null;
				Session["btNameMonAn"] = null;
				Session["btDvt_gia"] = null;
				Session["Stt_Rec"] = null;
				Session["STT_Rec0"] = null;
				Response.Redirect("~\\Invoice.aspx");
			}
		}

		protected void btGiam_Click(object sender, EventArgs e)
		{
			Button btn = (Button)sender;
			Session["STT_Rec0"] = btn.CommandArgument;
			Session["change"] = "3";
			Response.Redirect("~\\Invoice.aspx");
		}

		protected void btIn_Click(object sender, EventArgs e)
		{
			if (Session["Stt_Rec"] != null)
			{
				if (Session["dt"] != null)
				{
					SqlConnection proxyConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["proxyconn"].ConnectionString);
					proxyConn.Open();

					SqlCommand sqlComm = new SqlCommand("SELECT * FROM userinfo WHERE UPPER(UserID) = @UserID", proxyConn);
					sqlComm.CommandType = CommandType.Text;
					sqlComm.Parameters.Add("@UserID", SqlDbType.VarChar).Value = System.Web.HttpContext.Current.User.Identity.Name.ToUpper();
					SqlDataAdapter sqlDA = new SqlDataAdapter();
					sqlDA.SelectCommand = sqlComm;
					DataTable dtUserInfo = new DataTable();
					sqlDA.Fill(dtUserInfo);

					proxyConn.Close();
					proxyConn.Dispose();

					DataTable dtSoCT = new DataTable();
					dtSoCT = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT So_ct FROM ph81 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Stt_rec = '" + Session["Stt_Rec"].ToString() + "'");
					if (dtSoCT.Rows.Count < 1)
						return;

					DataTable dtHDA = (DataTable)Session["dt"];
					decimal tienhang = 0;
					decimal chietkhau = 0;
					bool checkExistCol = false;
					for (int i = 0; i < dtHDA.Columns.Count; i++)
						if (dtHDA.Columns[i].ColumnName == "Tien_hang")
						{
							checkExistCol = true;
							break;
						}
					if (checkExistCol == false)
						dtHDA.Columns.Add("Tien_hang", Type.GetType("System.Decimal"));
					for (int i = 0; i < dtHDA.Rows.Count; i++)
					{
						dtHDA.Rows[i]["Tien_hang"] = Convert.ToDecimal(dtHDA.Rows[i]["Nguyen_gia"]) * Convert.ToDecimal(dtHDA.Rows[i]["So_luong"]);
						tienhang = tienhang + (Convert.ToDecimal(dtHDA.Rows[i]["Nguyen_gia"]) * Convert.ToDecimal(dtHDA.Rows[i]["So_luong"]));
						chietkhau = chietkhau + (Convert.ToDecimal(dtHDA.Rows[i]["Gia_ck"]) * Convert.ToDecimal(dtHDA.Rows[i]["So_luong"]));
					}
					if (CommonFunction.GetReportType("HDA") == "0" || CommonFunction.GetReportType("HDA") == "1")
					{
						rptHDA rpt = new rptHDA { DataSource = dtHDA };
						rpt.VtName.DataBindings.Add("Text", dtHDA, "VtName");
						rpt.So_luong.DataBindings.Add("Text", dtHDA, "So_luong", "{0:#,#}");
						rpt.Nguyen_gia.DataBindings.Add("Text", dtHDA, "Nguyen_gia", "{0:#,#}");
						rpt.Tien.DataBindings.Add("Text", dtHDA, "Tien_hang", "{0:#,#}");

						rpt.Parameters["CtyName"].Value = dtUserInfo.Rows[0]["Company"].ToString();
						rpt.Parameters["CtyDiaChi"].Value = dtUserInfo.Rows[0]["Address"].ToString(); ;
						rpt.Parameters["CtyDienThoai"].Value = dtUserInfo.Rows[0]["Phone"].ToString(); ;
						rpt.Parameters["TenPhanMem"].Value = "Phần mềm HiVi 2S Cafe";
						rpt.Parameters["SoCT"].Value = dtSoCT.Rows[0]["So_ct"].ToString();
						rpt.Parameters["Kho"].Value = drKho.SelectedValue + " - " + drKho.SelectedItem.Text;
						rpt.Parameters["NgayThang"].Value = "Ngày " + DateTime.Now.Day.ToString() + " tháng " + DateTime.Now.Month.ToString() + " năm " + DateTime.Now.Year.ToString();

						rpt.Parameters["TienHang"].Value = tienhang.ToString("#,#0");
						rpt.Parameters["ChietKhau"].Value = chietkhau.ToString("#,#0");
						rpt.Parameters["ThanhToan"].Value = (tienhang - chietkhau).ToString("#,#0");

						rpt.PageHeight = 0;
						rpt.PageHeight = Convert.ToInt32(rpt.PageHeight + rpt.Bands[DevExpress.XtraReports.UI.BandKind.Detail].HeightF * (dtHDA.Rows.Count - 1)) + 1500;

						Session["Report"] = rpt;
						Response.Redirect("~\\Print.aspx");
					}
					else
					{
						rptHDA5 rpt = new rptHDA5 { DataSource = dtHDA };
						rpt.VtName.DataBindings.Add("Text", dtHDA, "VtName");
						rpt.So_luong.DataBindings.Add("Text", dtHDA, "So_luong", "{0:#,#}");
						rpt.Nguyen_gia.DataBindings.Add("Text", dtHDA, "Nguyen_gia", "{0:#,#}");
						rpt.Tien.DataBindings.Add("Text", dtHDA, "Tien_hang", "{0:#,#}");

						rpt.Parameters["CtyName"].Value = dtUserInfo.Rows[0]["Company"].ToString();
						rpt.Parameters["CtyDiaChi"].Value = dtUserInfo.Rows[0]["Address"].ToString(); ;
						rpt.Parameters["CtyDienThoai"].Value = dtUserInfo.Rows[0]["Phone"].ToString(); ;
						rpt.Parameters["TenPhanMem"].Value = "Phần mềm HiVi 2S Cafe";
						rpt.Parameters["NgayThang"].Value = "Ngày " + DateTime.Now.Day.ToString() + " tháng " + DateTime.Now.Month.ToString() + " năm " + DateTime.Now.Year.ToString();
						rpt.Parameters["TienHang"].Value = tienhang.ToString("#,#0");

						//rpt.PageHeight = 0;
						//rpt.PageHeight = Convert.ToInt32(rpt.PageHeight + rpt.Bands[DevExpress.XtraReports.UI.BandKind.Detail].HeightF * (dtHDA.Rows.Count - 1)) + 1500;

						Session["Report"] = rpt;
						Response.Redirect("~\\Print.aspx");
					}
				}
			}
		}


		protected void drKho_SelectedChanged(object sender, EventArgs objec)
		{
			if (drKho.SelectedValue != null)
			{
				Session["Kho"] = drKho.SelectedValue.ToString();
				if (Session["dt"] != null)
				{
					DataTable dt = Session["dt"] as DataTable;
					for (int i = 0; i < dt.Rows.Count; i++)
					{
						dt.Rows[i]["FK_KhoID"] = Session["Kho"].ToString();
					}
					dt.AcceptChanges();
					Session["dt"] = dt;
				}
			}
		}

		protected void EditInvoice(object sender, GridViewEditEventArgs e)
		{
			gdxdsmon.EditIndex = e.NewEditIndex;
			BlindGridView_EditCk();
		}

		protected void CancelEditInvoice(object sender, GridViewCancelEditEventArgs e)
		{
			gdxdsmon.EditIndex = -1;
			BlindGridView_EditCk();
		}

		protected void UpdateInvoice(object sender, GridViewUpdateEventArgs e)
		{
			DataTable dt = Session["dt"] as DataTable;
			int iRow = e.RowIndex;
			decimal soluong = Convert.ToDecimal(dt.Rows[iRow]["So_luong"].ToString());
			decimal nguyen_gia = Convert.ToDecimal(dt.Rows[iRow]["Nguyen_gia"].ToString());
			decimal ck = Convert.ToDecimal(((TextBox)gdxdsmon.Rows[iRow].FindControl("tbchietkhau")).Text);
			dt.Rows[iRow]["Ck"] = ck;
			dt.Rows[iRow]["Gia_ck"] = (nguyen_gia * ck) / 100;
			dt.Rows[iRow]["Gia"] = nguyen_gia - (nguyen_gia * ck) / 100;
			dt.Rows[iRow]["Tien"] = soluong * (nguyen_gia - (nguyen_gia * ck) / 100);
			dt.AcceptChanges();

			Session["dt"] = dt;
			gdxdsmon.EditIndex = -1;
			Session["change"] = "1";
			Response.Redirect("~\\Invoice.aspx");
		}

		protected void BlindGridView_EditCk()
		{
			gdxdsmon.DataSource = Session["dt"] as DataTable;
			gdxdsmon.DataBind();
		}

		protected void txtCKTong_TextChanged(object sender, EventArgs e)
		{
			if (Session["Stt_Rec"] != null)
			{
				if (Session["dt"] != null)
				{
					string tienhang = txtTienHang.Text.Replace(".", "").Replace(",", "");
					if (tienhang == "")
						tienhang = "0";
					string cktong = txtCKTong.Text.Replace(".", "").Replace(",", "");
					if (cktong == "")
						cktong = "0";
					txtCKTong.Text = Convert.ToDecimal(cktong).ToString("#,#0");
					txtTongTien.Text = (Convert.ToDecimal(tienhang) - Convert.ToDecimal(cktong)).ToString("#,#0");

					string khachdua = txtKhachDua.Text.Replace(".", "").Replace(",", "");
					if (khachdua == "")
						khachdua = "0";
					string tongtien = txtTongTien.Text.Replace(".", "").Replace(",", "");
					txtTraLai.Text = (Convert.ToDecimal(khachdua) - Convert.ToDecimal(tongtien)).ToString("#,#0");
				}
			}

		}
		protected void txtKhachDua_TextChanged(object sender, EventArgs e)
		{
			if (Session["Stt_Rec"] != null)
			{
				if (Session["dt"] != null)
				{
					string khachdua = txtKhachDua.Text.Replace(".", "").Replace(",", "");
					if (khachdua == "")
						khachdua = "0";
					txtKhachDua.Text = Convert.ToDecimal(khachdua).ToString("#,#0");
					string tongtien = txtTongTien.Text.Replace(".", "").Replace(",", "");
					if (tongtien == "")
						tongtien = "0";
					txtTraLai.Text = (Convert.ToDecimal(khachdua) - Convert.ToDecimal(tongtien)).ToString("#,#0");
				}
			}
		}

		protected void btnDoiTacSearch_Click(object sender, EventArgs e)
		{
			DataTable dtDoiTac = new DataTable();
			dtDoiTac = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT DoiTacID, DoiTacName, DoiTacTel FROM dmdoitac WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Status = '1' AND Kh_yn = 1 AND (DoiTacID LIKE '%" + txtDoiTac.Text + "%' OR DoiTacName LIKE N'%" + txtDoiTac.Text + "%' OR DoiTacTel LIKE '%" + txtDoiTac.Text + "%')");
			drDoiTac.DataSource = dtDoiTac;
			drDoiTac.DataValueField = "DoiTacID";
			drDoiTac.DataTextField = "DoiTacName";
			drDoiTac.DataBind();

			if (dtDoiTac.Rows.Count > 0)
			{
				drDoiTac.SelectedIndex = 0;
				if (dtDoiTac.Rows[0]["DoiTacTel"].ToString().Trim() != "")
					lblDoiTac.Text = dtDoiTac.Rows[0]["DoiTacID"].ToString().ToUpper() + " - " + dtDoiTac.Rows[0]["DoiTacTel"].ToString();
				else
					lblDoiTac.Text = dtDoiTac.Rows[0]["DoiTacID"].ToString().ToUpper();
			}
		}

		protected void drDoiTac_SelectedChanged(object sender, EventArgs objec)
		{
			if (drDoiTac.SelectedValue != null)
			{
				DataTable dtDoiTac = new DataTable();
				dtDoiTac = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT DoiTacTel FROM dmdoitac WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND DoiTacID = '" + drDoiTac.SelectedValue.ToString() + "' AND Status = '1'");
				if (dtDoiTac.Rows[0]["DoiTacTel"].ToString().Trim() != "")
					lblDoiTac.Text = drDoiTac.SelectedValue.ToString().ToUpper() + " - " + dtDoiTac.Rows[0]["DoiTacTel"].ToString();
				else
					lblDoiTac.Text = drDoiTac.SelectedValue.ToString().ToUpper();
			}
		}
	}
}
